package pkgIfx_Conexiones;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.neurotechnology.NImages.NImage;
import com.neurotechnology.NImages.NImageFormat;
import com.neurotechnology.Templates.NLTemplate;
import java.sql.SQLException;
import adirector.cIfxDatosTemplate;

public class cIfxBD_Rostro
{
    private Connection con;

    public cIfxBD_Rostro() throws Exception
    {
        Class.forName("org.gjt.mm.mysql.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/faces", "root", "9912056");
    }

    public synchronized void append(String name, NImage image, NLTemplate template) throws Exception
    {
        PreparedStatement pstmt = con.prepareStatement("Select MAX(Id) from Templates");
        int maxID = pstmt.executeQuery().getInt(1);
        byte[] pngimage = null;
        for (NImageFormat format : NImageFormat.getFormats())
        if (format.getName().contains("PNG"))
            pngimage = format.saveToMemory(image);
        pstmt = con.prepareStatement("INSERT INTO Templates VALUES (?,?,?,?)");
            pstmt.setInt(1, maxID + 1);
        if (name != null)
            pstmt.setString(2, name);
        if (template != null)
            pstmt.setBytes(3, template.toByteArray());
        if (pngimage != null)
            pstmt.setBytes(4, null);
        pstmt.execute();
    }

    public String[] Face(String cedulaBus)
    {
        String consulta[] = new String[4];
        PreparedStatement stmt = null; //statement para el sql
        try
        {
            stmt = con.prepareStatement("SELECT * FROM Templates where TemplateId ='"+cedulaBus+"'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next())
            {
                for (int row = 0; row <= 3; row++) {
                consulta[row] = rs.getString(row+1);
            }
        }
        rs.close();
        con.close();
        }
        catch (Exception e)
        {
            System.out.println("Error en consulta de cedula BD: "+e);
            return consulta;
        }
        return consulta;
    }

    public cIfxDatosTemplate bus_fac_Images(String cedula) throws SQLException
    {
        cIfxDatosTemplate ofceimge = new cIfxDatosTemplate();
        PreparedStatement stmt = null;
        try
        {
            stmt = con.prepareStatement("SELECT * FROM Templates where TemplateId ='"+cedula+"'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next())
            {
                ofceimge.T_Id=rs.getInt(1);
                ofceimge.T_Template_Id=rs.getString(2);
                ofceimge.T_Template= rs.getBytes(3);
                ofceimge.T_Thumbnail= rs.getBytes(4);
            }
            rs.close();
        }
        catch (Exception e)
        {
            System.out.println("Error en consulta de cedula BD 3: "+e);
            return ofceimge;
        }
        System.out.println("gggg "+ofceimge.ID);
        return ofceimge;
    }

    public void Borrar_Rostro(String cedula)
    {
        try {
            Statement delete = con.createStatement();
            delete.executeUpdate("DELETE FROM Templates WHERE TemplateId = '"+cedula+"'");
        }
        catch (SQLException e)
        {
            System.out.println(e.getMessage());
            System.out.println(e);
        }
    }
}
